import sys
from loguru import logger as loguru_logger
from app.settings import settings

class Loggin:
    def __init__(self) -> None:
        # 根据 DEBUG 配置动态设置日志级别
        self.level = "DEBUG" if settings.DEBUG else "INFO"

    def setup_logger(self):
        # 移除所有默认的日志处理器
        loguru_logger.remove()

        # 输出日志到控制台
        loguru_logger.add(sys.stdout, level=self.level, format="{time} {level} {message}", colorize=True)

        # 输出日志到文件，并设置日志文件大小限制，超过后进行轮转
        loguru_logger.add("shop.log", level=self.level, rotation="100 MB", retention="10 days", compression="zip")

        return loguru_logger

# 初始化日志记录器
loggin = Loggin()
logger = loggin.setup_logger()
